#!/usr/bin/env python
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.2')

import wsgiref.handlers
from google.appengine.ext import webapp
import logging

from model import StudentM
from model import PostClassM
from model import LectureM
from model import QuestionM

class StudentVoiceHandler(webapp.RequestHandler):
    
    def post(self):
#        studentKey=self.request.get('studentKey')
        lectureKey=self.request.get('lectureKey')
        questionKey=self.request.get('questionKey')
#        logging.debug("\n\n\nStudentVoiceHandler, lectureKey: "+lectureKey+"\n\n\n")
        question=QuestionM.getQuestionByKey(questionKey)
        questiondata=StudentM.getQuestionData(questionKey)
        lecture=LectureM.getLectureByKey(lectureKey)
        result = PostClassM.saveQuestionStats(lecture, question, questiondata)
        StudentM.deleteOldStudentAnswers(question)
#       
        self.response.out.write(result)
        
def main():
    logging.getLogger().setLevel(logging.DEBUG)
    app = webapp.WSGIApplication([
        (r'.*', StudentVoiceHandler)], debug=True)
    wsgiref.handlers.CGIHandler().run(app)

if __name__ == "__main__":
    main()